﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMS.HelperClasses;
using WHMS.HelperClasses;
using WHMS.EntityClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using WHMS.DatabaseSpecific;
using StringCryptography;

namespace WMS
{
	public partial class UserForm : Form
	{
		public UserForm()
		{
			InitializeComponent();

			var dataForm = new DataMngrFormBase<UserEntity>(_users, dataGridView1, bindingSource1, SaveBtn, SaveResult, DeleteResult);			
			dataForm.SetMNInfo(typeof(UserRoleEntity), AllRolesList, AssignedRolesList, RoleFields.Id, RoleFields.Name, UserRoleFields.RoleId, UserRoleFields.UserId);
			dataForm.SetMNAssociationButtons(AssignSelectedBtn, AssignAllBtn, UnAssignSelectedBtn, UnAssignAllBtn, SaveDetailsBtn);
			dataForm.MNSaved += new DataMngrFormBase<UserEntity>.RaiseMNSaved(dataForm_MNSaved);
			dataForm.SelectionChanged += new DataMngrFormBase<UserEntity>.RaiseSelectionChanged(dataForm_SelectionChanged);
			dataForm.SetMandatoryFields(new List<int>() { 0, 1, 2 });

			var uniqueFields = new Dictionary<int, EntityField2>();
			uniqueFields.Add(0, UserFields.Code);
			uniqueFields.Add(1, UserFields.Name);
			uniqueFields.Add(2, UserFields.UserName);
			dataForm.SetUniqueFields(uniqueFields, UserFields.Id);
		}

		void dataForm_MNSaved(int selectedId)
		{
			if (dataGridView1.SelectedCells.Count <= 0)
				return;

			int id;
			if (!int.TryParse(dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex].Cells[dataGridView1.Columns.Count - 1].Value.ToString(), out id))
				return;

			if (string.IsNullOrEmpty(pswdValidation.GetError(Pswd2TB)) && string.IsNullOrEmpty(pswdValidation.GetError(PswdTB)))
			{
				if (Business.SavePassword(id, PswdTB.Text))
				{
					MessageBox.Show("تم الحفظ بنجاح");
				}
			}			
		}

		void dataForm_SelectionChanged(int selectedRowIndex)
		{
			var userName = dataGridView1.Rows[selectedRowIndex].Cells["UserName"].Value;
			UserNameTB.Text = (userName == null) ? string.Empty : userName.ToString();

			//var pswd = dataGridView1.Rows[selectedRowIndex].Cells[4].Value;
			//Pswd2TB.Text = (pswd == null) ? string.Empty : pswd.ToString();
			//PswdTB.Text = Pswd2TB.Text;
			var selectedValue = dataGridView1.Rows[selectedRowIndex].Cells[dataGridView1.ColumnCount - 1].Value;
			var currentId = (selectedValue == null) ? 0 : int.Parse(selectedValue.ToString());

			if (currentId == 0)
			{
				Pswd2TB.Enabled = false;
				PswdTB.Enabled = false;
			}
			else
			{
				Pswd2TB.Enabled = true;
				PswdTB.Enabled = true;
			}

		}

		private void Pswd2TB_Validating(object sender, CancelEventArgs e)
		{
			if (Pswd2TB.Text == PswdTB.Text)
			{
				pswdValidation.SetError(Pswd2TB, string.Empty);
				return;
			}

			pswdValidation.SetError(Pswd2TB, "كلمة السر غير متطابقة");
		}

		private void PswdTB_Validating(object sender, CancelEventArgs e)
		{
			if (PswdTB.Text.Trim().Length >= 8)
			{
				pswdValidation.SetError(PswdTB, string.Empty);
				return;
			}
			pswdValidation.SetError(PswdTB, "كلمة السر يجب أن تحتوى على ثمان أحرف على الأقل");

		}
	}
}
